package com.app.service.impl;

import com.app.mapper.AdminUserMapper;
import com.app.mapper.MySqlMapper;
import com.app.model.AdminUserModel;
import com.app.model.MysqlModel;
import com.app.service.AdminUserService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.ListUtils;
import org.thymeleaf.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Primary
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUserModel> implements AdminUserService {
    @Autowired
    AdminUserMapper adminUserMapper;

    @Override
    public AdminUserModel getUserByUserName(String account, String pwd) {
        QueryWrapper<AdminUserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("account", account).eq("pwd", pwd);
        try {
            return adminUserMapper.selectOne(queryWrapper);
        } catch (Exception e) {
            AdminUserModel model = new AdminUserModel();
            model.setRe(1);
            return model;
        }
    }

    @Override
    public AdminUserModel getUserByUserId(int id) {
        return adminUserMapper.selectById(id);
    }

    @Override
    public Page<AdminUserModel> getPageUserList(int page, int limit, String account) {
        QueryWrapper<AdminUserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "account", "roles", "real_name", "ip_address", "mac_address", "last_ip", "last_login_time", "status", "department_id", "person_id");
        if (account != null) {
            queryWrapper.like("account", account);
        }
        queryWrapper.orderByAsc("id");
        Page<AdminUserModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<AdminUserModel> userList = adminUserMapper.selectPage(pages, queryWrapper);
        return userList;
    }

    @Override
    public List<AdminUserModel> userList() {
        QueryWrapper<AdminUserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "account", "roles", "real_name", "ip_address", "mac_address", "last_ip", "last_login_time", "status", "department_id", "person_id");
        List<AdminUserModel> userModelList = adminUserMapper.selectList(queryWrapper);
        return userModelList;
    }

    @Override
    public void update(AdminUserModel model) {
        adminUserMapper.updateById(model);
    }

    @Override
    public void insert(AdminUserModel model) {
        adminUserMapper.insert(model);
    }

    @Override
    public void delete(int id) {
        adminUserMapper.deleteById(id);
    }

    @Override
    public AdminUserModel getUserByName(String account) {
        QueryWrapper<AdminUserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "account", "roles", "real_name", "ip_address", "mac_address", "last_ip", "last_login_time", "status", "department_id", "person_id");
        queryWrapper.eq("account", account);
        AdminUserModel user = adminUserMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public AdminUserModel getUserByPersonId(Integer id) {
        QueryWrapper<AdminUserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("person_id", id);
        return adminUserMapper.selectOne(queryWrapper);
    }


}
